var communication = {
    ws: null,
    init: function (options) {
        if (null == options) {
            console.error("参数不能为空");
            return;
        }
        if (options.url == null || '' == options.url) {
            console.error("url参数不能为空");
            return;
        }
        if (options.onmessage == null) {
            console.error("onmessage参数不能为空");
            return;
        }
        ws = new WebSocket(options.url);

        var heartCheck = {
            timeout: 60000,//60s
            timeoutObj: null,
            serverTimeoutObj: null,
            reset: function () {
                //收到websocket消息,重置心跳检测定时器
                clearTimeout(this.timeoutObj);
                clearTimeout(this.serverTimeoutObj);
                this.start();
            },
            start: function () {
                //心跳检测,60s内没有收到服务器消息,会向服务器发送一条心跳消息,发送之后60s收不到服务器消息则重连
                var self = this;
                this.timeoutObj = setTimeout(function () {
                    var object = {};
                    object.code = 200;
                    ws.send(JSON.stringify(object));
                    self.serverTimeoutObj = setTimeout(function () {
                        ws.close();//如果onclose会执行reconnect，我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
                    }, self.timeout)
                }, this.timeout)
            },
        };

        initWs();

        function initWs() {
            ws.onopen = function () {
                //心跳检测
                reconnet = true;
                heartCheck.start();
            };
            ws.onmessage = function (event) {
                //收到服务端消息,重置心跳定时器
                heartCheck.reset();
                var data = JSON.parse(event.data);
                if (data.code == 200) {
                    //心跳消息不需要
                    console.log("心跳检测消息");
                    return;
                }
                if (data.code >= 400) {
                    //大于等于400的code属于异常，需要主动断开websocket连接
                    console.error("异常消息" + event.data);
                    ws.onclose = null;
                    ws.onerror = null;
                    clearTimeout(heartCheck.timeoutObj);
                    clearTimeout(heartCheck.serverTimeoutObj);
                }
                options.onmessage(event.data);
            };
            ws.onclose = function () {
                //close时重新连接
                if (reconnet) {
                    reconnect();
                }
            };
            ws.onerror = function () {
                //错误时重新连接
                if (reconnet) {
                    reconnect();
                }
            };
        }


        function reconnect() {
            //重现连接代码
            ws.onopen = null;
            ws.onmessage = null;
            ws.onclose = null;
            ws.onerror = null;
            clearTimeout(heartCheck.timeoutObj);
            clearTimeout(heartCheck.serverTimeoutObj);
            reconnet = false;
            ws = new WebSocket(options.url);
            initWs();
        }
    },
    send: function (data) {
        if (ws == null) {
            console.error("未初始化ws");
            return;
        }
        ws.send(data);
    },

    sendFile: function (data) {
        if (data.input == null || '' == data.input) {
            console.error("需要传入文件");
            return;
        }
        var form = new FormData();
    }
};
